<?php
namespace app\controller;

use app\model\UserModel;
use think\facade\View;
use think\Request;

class User
{
    // 显示所有用户
    public function index()
    {
        $list = UserModel::select();
        return View::fetch('index', ['list' => $list]);
    }

    // 显示创建表单
    public function create()
    {
        return View::fetch('create');
    }

    // 保存新用户
    public function save(Request $request)
    {
        $data = $request->post();
        UserModel::create($data);
        return redirect('/user');
    }

    // 显示编辑表单
    public function edit($id)
    {
        $user = UserModel::find($id);
        return View::fetch('edit', ['user' => $user]);
    }

    // 更新用户
    public function update(Request $request, $id)
    {
        $data = $request->post();
        UserModel::update($data, ['id' => $id]);
        return redirect('/user');
    }

    // 删除用户
    public function delete($id)
    {
        UserModel::destroy($id);
        return redirect('/user');
    }
}